﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BathhouseService.Models.Commonality
{
    /// <summary>
    /// 日期计算
    /// </summary>
    public static class DateCalculation
    {
        /// <summary>
        /// 月初
        /// </summary>
        /// <param name="dateTime">月份任意一天</param>
        /// <returns></returns>
        public static DateTime StartMonth(DateTime dateTime)
        {
            DateTime datetemp = dateTime.AddDays(1 - dateTime.Day);
            return Convert.ToDateTime(datetemp.ToShortDateString() + " 00:00:00");
        }
        /// <summary>
        /// 月底
        /// </summary>
        /// <param name="dateTime">月份任意一天</param>
        /// <returns></returns>
        public static DateTime EndMonth(DateTime dateTime)
        {
            DateTime startMonth = StartMonth(dateTime);
            DateTime datetemp = startMonth.AddMonths(1).AddDays(-1);
            return Convert.ToDateTime(datetemp.ToShortDateString() + " 23:59:59");
        }
        /// <summary>
        /// 获取当年1-12月，月初和月底
        /// </summary>
        /// <param name="year"></param>
        /// <returns></returns>
        public static List<YearStartEndMonth> AllStartEndMonth(string year)
        {
            List<YearStartEndMonth> list = new List<YearStartEndMonth>();
            for (int i = 1; i < 13; i++)
            {
                string dataStr = string.Empty;
                if (i<10)
                {
                    dataStr = year + "-0" + i.ToString() + "-05";
                }
                else
                {
                    dataStr = year + "-" + i.ToString() + "-05";
                }

                YearStartEndMonth item = new YearStartEndMonth
                {
                    Id = i,
                    StartMonth = StartMonth(Convert.ToDateTime(dataStr)),
                    EndMonth = EndMonth(Convert.ToDateTime(dataStr))
                };
                list.Add(item);
            }
            return list;
        }
    }
    public class YearStartEndMonth
    {
        public int Id { get; set; }
        public DateTime StartMonth { get; set; }
        public DateTime EndMonth { get; set; }
    }
}
